/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bibliOnline.util.details;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author luca
 */
public final class BookDetails implements Serializable {

    public static final String PROP_AUTHOR = "author";
    private String author;
    public static final String PROP_TITLE = "title";
    private String title;
    public static final String PROP_PUBLISHER = "publisher";
    private String publisher;
    public static final String PROP_EDITION = "edition";
    private String edition;
    public static final String PROP_GENRE = "genre";
    private String genre;
    public static final String PROP_YEAR = "year";
    private int year;

    public BookDetails() {
    }

    public BookDetails(String author, String title, String publisher,
            String edition, String genre, int year) throws Exception {
        setAuthor(author);
        setTitle(title);
        setPublisher(publisher);
        setEdition(edition);
        setGenre(genre);
        setYear(year);
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getEdition() {
        return edition;
    }

    public void setEdition(String edition) {
        this.edition = edition;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void checkAndSetAuthor(String author) throws Exception {
        if (author == null || author.equals("")) {
            throw new Exception("Autore: attributo obbligatorio");
        }

        if (author.length() >= 50) {
            throw new Exception("Autore: lunghezza massima 50 caratteri");
        }

        String description = "una lettera maiuscola, poi da 1 a 50 lettere o apici, "
                + "poi 0 o piu' coppie spazio-lettere";
        String regex = "[A-Z][a-zA-z']{1,50}( [a-zA-z']{1,50}){0,}";

        Pattern pattern;
        pattern = Pattern.compile(regex);
        Matcher matcher;
        matcher = pattern.matcher(author);
        if (matcher.matches() == false) {
            throw new Exception("Autore: " + description);
        }

        this.author = author;
    }

    public void checkAndSetEdition(String edition) throws Exception {
        this.edition = edition;
    }

    public void checkAndSetGenre(String genre) throws Exception {
        this.genre = genre;
    }

    public void checkAndSetPublisher(String publisher) throws Exception {
        if (publisher == null || publisher.equals("")) {
            this.publisher = "";
            return;
        }

        if (publisher.length() >= 50) {
            throw new Exception("Editore: lunghezza massima 50 caratteri");
        }

        String description = "una lettera maiuscola, poi da 1 a 50 lettere o apici, "
                + "poi 0 o piu' coppie spazio-lettere";
        String regex = "[A-Z][a-zA-z']{0,50}( [a-zA-z']{1,50}){0,}";

        Pattern pattern;
        pattern = Pattern.compile(regex);
        Matcher matcher;
        matcher = pattern.matcher(publisher);
        if (matcher.matches() == false) {
            throw new Exception("Editore: " + description);
        }

        this.publisher = publisher;
    }

    public void checkAndSetTitle(String title) throws Exception {
        if (title == null || title.equals("")) {
            throw new Exception("Titolo: attributo obbligatorio");
        }

        if (title.length() >= 150) {
            throw new Exception("Titolo: lunghezza massima 150 caratteri");
        }

        String description = "una lettera maiuscola, poi da 1 a 50 lettere o apici, "
                + "poi 0 o piu' coppie spazio-lettere";
        String regex = "[A-Z][a-zA-z'\"]{0,50}( [a-zA-z'\"]{1,50}){0,}";

        Pattern pattern;
        pattern = Pattern.compile(regex);
        Matcher matcher;
        matcher = pattern.matcher(title);
        if (matcher.matches() == false) {
            throw new Exception("Titolo: " + description);
        }

        this.title = title;
    }

    public void checkAndSetYear(String year) throws Exception {
        if (year == null || year.equals("")) {
            this.year = 0;
            return;
        }

        int parsedYear;
        try {
            parsedYear = Integer.parseInt(year);
        } catch (Exception ex) {
            throw new Exception("Anno: valore non intero");
        }
        this.year = parsedYear;
    }
}
